import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import {HttpServies} from '../../providers/http-servies';
import 'rxjs/add/operator/toPromise';
import {UserPage} from "../user/user";




@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
  providers:[HttpServies]
})
export class LoginPage {
  userInfo = {
    'mobile': '',
    'password': '',
    'pwd': ''

  }
  constructor(public navCtrl: NavController,private http:HttpServies, public navParams: NavParams) {
  }

  checkInfo() {
    if (!this.userInfo.mobile || !this.userInfo.pwd || !this.userInfo.password ) {
      this.http.showErro("请输入完整信息");
      return;
    }
    if (this.userInfo.mobile) {
      var myreg = /^1[3|4|5|7|8|9]\d{9}$/;
      if (!myreg.test(this.userInfo.mobile)) {
        this.http.showErro("请输入正确的手机号码");
        return;
      }
    }
    var param = "mobile=" + this.userInfo.mobile + "&pwd=" + this.userInfo.pwd +"&password=" + this.userInfo.password;
    this.http.httpPost('/home/mine/register', param)
        .then(res => {
          if (res && res.code ==1) {
            this.http.showErro(res.msg);
            console.log(res.msg);
            localStorage.setItem("mobile",this.userInfo.mobile);
            localStorage.setItem("token",res.data);
            this.navCtrl.push(UserPage);
          }else {
            this.http.showErro(res.msg);
          }
        })
  }
  /*
   * 验证码倒计时
   *
   *
   * */
  verifyCode: any = {
    verifyCodeTips: "获取验证码",
    countdown: 10,
    disable: true
  }
  // 倒计时
  settime() {
    if (this.verifyCode.countdown == 1) {
      this.verifyCode.countdown = 10;
      this.verifyCode.verifyCodeTips = "获取验证码";
      this.verifyCode.disable = true;
      return;
    } else {
      this.verifyCode.countdown--;
    }

    this.verifyCode.verifyCodeTips = "重新获取(" + this.verifyCode.countdown + ")";
    setTimeout(() => {
      this.verifyCode.verifyCodeTips = "重新获取(" + this.verifyCode.countdown + ")";
      this.settime();
    }, 1000);
  }
  /*
   * 验证码方法
   *
   * */
  doHome() {
    this.verifyCode.disable = false;
    this.settime();
    var param = "mobile=" + this.userInfo.mobile;
    this.http.httpPost('/home/mine/authcode', param)
        .then(res => {
          if (res && res.code == 1) {
            this.http.showErro(res.msg);
            console.log(res.msg);
          }else {
            this.http.showErro(res.msg);
          }
        })

  }
}
